{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 数据读取及基本处理\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "from sklearn.metrics import mean_squared_error"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 731 entries, 0 to 730\n",
      "Data columns (total 34 columns):\n",
      "season_1        731 non-null int64\n",
      "season_2        731 non-null int64\n",
      "season_3        731 non-null int64\n",
      "season_4        731 non-null int64\n",
      "mnth_1          731 non-null int64\n",
      "mnth_2          731 non-null int64\n",
      "mnth_3          731 non-null int64\n",
      "mnth_4          731 non-null int64\n",
      "mnth_5          731 non-null int64\n",
      "mnth_6          731 non-null int64\n",
      "mnth_7          731 non-null int64\n",
      "mnth_8          731 non-null int64\n",
      "mnth_9          731 non-null int64\n",
      "mnth_10         731 non-null int64\n",
      "mnth_11         731 non-null int64\n",
      "mnth_12         731 non-null int64\n",
      "weathersit_1    731 non-null int64\n",
      "weathersit_2    731 non-null int64\n",
      "weathersit_3    731 non-null int64\n",
      "weekday_0       731 non-null int64\n",
      "weekday_1       731 non-null int64\n",
      "weekday_2       731 non-null int64\n",
      "weekday_3       731 non-null int64\n",
      "weekday_4       731 non-null int64\n",
      "weekday_5       731 non-null int64\n",
      "weekday_6       731 non-null int64\n",
      "temp            731 non-null float64\n",
      "atemp           731 non-null float64\n",
      "hum             731 non-null float64\n",
      "windspeed       731 non-null float64\n",
      "holiday         731 non-null int64\n",
      "workingday      731 non-null int64\n",
      "yr              731 non-null int64\n",
      "cnt             731 non-null int64\n",
      "dtypes: float64(4), int64(30)\n",
      "memory usage: 194.2 KB\n"
     ]
    }
   ],
   "source": [
    "data=pd.read_csv(\"FE_train.csv\")\n",
    "data.info()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.linear_model import LinearRegression,RidgeCV,LassoCV"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "X=data.drop('cnt',axis=1)\n",
    "y=data['cnt']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.model_selection import train_test_split"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(584, 33)"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X_train,X_test,y_train,y_test=train_test_split(X,y,random_state=33,test_size=0.2)\n",
    "X_train.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "lr=LinearRegression()\n",
    "lr.fit(X_train,y_train)\n",
    "y_train_pred_lr=lr.predict(X_train)\n",
    "y_test_pred_lr=lr.predict(X_test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The rmse of LinearRegression on train is  745.1558584783841\n",
      "The rmse of LinearRegression on test is  816.0783984280876\n"
     ]
    }
   ],
   "source": [
    "import math\n",
    "rmse_train=math.sqrt(mean_squared_error(y_train,y_train_pred_lr))\n",
    "rmse_test=math.sqrt(mean_squared_error(y_test,y_test_pred_lr))\n",
    "print(\"The rmse of LinearRegression on train is \",rmse_train)\n",
    "print(\"The rmse of LinearRegression on test is \",rmse_test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The r2_score of LinearRegression on train is  0.8506871964113922\n",
      "The r2_score of LinearRegression on test is  0.8272693015528699\n"
     ]
    }
   ],
   "source": [
    "from sklearn.metrics import r2_score  #评价回归预测模型的性能\n",
    "print(\"The r2_score of LinearRegression on train is \",r2_score(y_train,y_train_pred_lr))\n",
    "print(\"The r2_score of LinearRegression on test is \",r2_score(y_test,y_test_pred_lr))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "可以看到最小二乘线性回归效果"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.linear_model import  RidgeCV\n",
    "\n",
    "#1. 设置超参数（正则参数）范围\n",
    "#alphas = [ 0.01, 0.1, 1, 10,100]\n",
    "n_alphas = 20\n",
    "alphas = np.logspace(-5,2,n_alphas)\n",
    "ridge = RidgeCV(alphas=alphas, store_cv_values=True)  \n",
    "\n",
    "#3. 模型训练\n",
    "ridge.fit(X_train, y_train)    \n",
    "\n",
    "#4. 预测\n",
    "y_test_pred_ridge = ridge.predict(X_test)\n",
    "y_train_pred_ridge = ridge.predict(X_train)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The rmse of RidgeRegression on train is  747.1847099488855\n",
      "The rmse of RidgeRegression on test is  810.4978324804321\n"
     ]
    }
   ],
   "source": [
    "rmse_train=math.sqrt(mean_squared_error(y_train,y_train_pred_ridge))\n",
    "rmse_test=math.sqrt(mean_squared_error(y_test,y_test_pred_ridge))\n",
    "print(\"The rmse of RidgeRegression on train is \",rmse_train)\n",
    "print(\"The rmse of RidgeRegression on test is \",rmse_test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The r2_score of RidgeRegression on train is  0.8498730153259999\n",
      "The r2_score of RidgeRegression on test is  0.8296235832756609\n"
     ]
    }
   ],
   "source": [
    "print(\"The r2_score of RidgeRegression on train is \",r2_score(y_train,y_train_pred_ridge))\n",
    "print(\"The r2_score of RidgeRegression on test is \",r2_score(y_test,y_test_pred_ridge))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "岭回归在测试集上表现比最小二乘线性回归好上一些。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAaEAAAEKCAYAAAC7c+rvAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDMuMC4zLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvnQurowAAIABJREFUeJzt3XuUXWV9//H3d+65XycBc7eES0DAMHKpiNRwCS5rkIqCVrIov6a6sD+t9VdBVxsrbZcuW6mosMSSAi0FKeoPqigmQY36k8uEexggI8wkQ8JckzCZyVzOOd/fH/s5yclw5po5Z58z83mtddY557ufvZ/voc18ffZ+9rPN3REREYlDSdwJiIjI5KUiJCIisVEREhGR2KgIiYhIbFSEREQkNipCIiISGxUhERGJTc6KkJltMrMWM3shI/Z1M3vJzJ4zsx+Z2eyMbTeaWb2ZvWxml2bE14ZYvZndkBFfYWaPm9lOM/u+mVWEeGX4Xh+2Lx+uDxERiUcuR0J3AmsHxDYDp7n76cArwI0AZrYKuAo4Nexzq5mVmlkp8B3gMmAVcHVoC/A14GZ3XwnsA64L8euAfe5+AnBzaDdoH+P9o0VEZOTKcnVgd9+WOQoJsZ9nfH0M+HD4vA64z917gdfMrB44O2yrd/dXAczsPmCdmdUB7wM+FtrcBXwZuC0c68sh/gDwbTOzIfr43VC/Y/78+b58+fKhmoiIyADbt29vc/fq4drlrAiNwJ8B3w+fFxEVpbSmEAPYPSB+DjAP2O/uiSztF6X3cfeEmR0I7YfqY1DLly+ntrZ2hD9JREQAzKxxJO1imZhgZl8CEsA96VCWZj6G+FiOlS2/DWZWa2a1ra2t2ZqIiMg4yHsRMrP1wAeAj/uR1VObgCUZzRYDe4aItwGzz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oKIiMRGRUhERGKjIiQiIrFRERIRkdioCImISGxUhEREJDb/H3yvqRMCG9k/AAAAAElFTkSuQmCC\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "alpha is: 1.438449888287663\n"
     ]
    }
   ],
   "source": [
    "import matplotlib.pyplot as plt   #画图\n",
    "import seaborn as sns\n",
    "\n",
    "# 图形出现在Notebook里而不是新窗口\n",
    "%matplotlib inline\n",
    "mse_mean = np.mean(ridge.cv_values_, axis = 0)\n",
    "plt.plot(np.log10(alphas), mse_mean.reshape(len(alphas),1)) \n",
    "\n",
    "#这是为了标出最佳参数的位置，不是必须\n",
    "#plt.plot(np.log10(ridge.alpha_)*np.ones(3), [0.28, 0.29, 0.30])\n",
    "\n",
    "plt.xlabel('log(alpha)')\n",
    "plt.ylabel('mse')\n",
    "plt.show()\n",
    "\n",
    "print ('alpha is:', ridge.alpha_)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "feat_names=X.columns"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>columns</th>\n",
       "      <th>coef_lr</th>\n",
       "      <th>coef_ridge</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>mnth_9</td>\n",
       "      <td>3.541269e+15</td>\n",
       "      <td>762.241574</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>mnth_5</td>\n",
       "      <td>3.541269e+15</td>\n",
       "      <td>329.974814</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>mnth_6</td>\n",
       "      <td>3.541269e+15</td>\n",
       "      <td>358.441707</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>mnth_8</td>\n",
       "      <td>3.541269e+15</td>\n",
       "      <td>260.186161</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>mnth_3</td>\n",
       "      <td>3.541269e+15</td>\n",
       "      <td>158.256991</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>mnth_10</td>\n",
       "      <td>3.541269e+15</td>\n",
       "      <td>228.422168</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>mnth_4</td>\n",
       "      <td>3.541269e+15</td>\n",
       "      <td>-0.717209</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>mnth_2</td>\n",
       "      <td>3.541269e+15</td>\n",
       "      <td>-321.473059</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>mnth_7</td>\n",
       "      <td>3.541269e+15</td>\n",
       "      <td>-219.802644</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>mnth_1</td>\n",
       "      <td>3.541269e+15</td>\n",
       "      <td>-419.602984</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>mnth_11</td>\n",
       "      <td>3.541269e+15</td>\n",
       "      <td>-539.626153</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15</th>\n",
       "      <td>mnth_12</td>\n",
       "      <td>3.541269e+15</td>\n",
       "      <td>-596.301365</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>16</th>\n",
       "      <td>weathersit_1</td>\n",
       "      <td>2.457579e+15</td>\n",
       "      <td>917.687059</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17</th>\n",
       "      <td>weathersit_2</td>\n",
       "      <td>2.457579e+15</td>\n",
       "      <td>376.129076</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>18</th>\n",
       "      <td>weathersit_3</td>\n",
       "      <td>2.457579e+15</td>\n",
       "      <td>-1293.816135</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25</th>\n",
       "      <td>weekday_6</td>\n",
       "      <td>8.319782e+14</td>\n",
       "      <td>222.343389</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>19</th>\n",
       "      <td>weekday_0</td>\n",
       "      <td>8.319782e+14</td>\n",
       "      <td>-191.110834</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>31</th>\n",
       "      <td>workingday</td>\n",
       "      <td>4.314588e+14</td>\n",
       "      <td>210.970710</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>30</th>\n",
       "      <td>holiday</td>\n",
       "      <td>4.314588e+14</td>\n",
       "      <td>-242.203266</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>24</th>\n",
       "      <td>weekday_5</td>\n",
       "      <td>4.005193e+14</td>\n",
       "      <td>84.232912</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>22</th>\n",
       "      <td>weekday_3</td>\n",
       "      <td>4.005193e+14</td>\n",
       "      <td>59.300844</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23</th>\n",
       "      <td>weekday_4</td>\n",
       "      <td>4.005193e+14</td>\n",
       "      <td>62.856322</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>21</th>\n",
       "      <td>weekday_2</td>\n",
       "      <td>4.005193e+14</td>\n",
       "      <td>-32.498610</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20</th>\n",
       "      <td>weekday_1</td>\n",
       "      <td>4.005193e+14</td>\n",
       "      <td>-205.124023</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>26</th>\n",
       "      <td>temp</td>\n",
       "      <td>2.538546e+03</td>\n",
       "      <td>1708.293961</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>32</th>\n",
       "      <td>yr</td>\n",
       "      <td>2.018267e+03</td>\n",
       "      <td>2018.537056</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>27</th>\n",
       "      <td>atemp</td>\n",
       "      <td>1.084552e+03</td>\n",
       "      <td>1531.373934</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>season_4</td>\n",
       "      <td>8.371710e+02</td>\n",
       "      <td>764.731124</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>season_2</td>\n",
       "      <td>8.215287e+01</td>\n",
       "      <td>109.430160</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>season_3</td>\n",
       "      <td>-1.453643e+02</td>\n",
       "      <td>-61.424018</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>season_1</td>\n",
       "      <td>-7.788613e+02</td>\n",
       "      <td>-812.737266</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>29</th>\n",
       "      <td>windspeed</td>\n",
       "      <td>-1.199925e+03</td>\n",
       "      <td>-1047.237700</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>28</th>\n",
       "      <td>hum</td>\n",
       "      <td>-1.351891e+03</td>\n",
       "      <td>-1054.590078</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "         columns       coef_lr   coef_ridge\n",
       "12        mnth_9  3.541269e+15   762.241574\n",
       "8         mnth_5  3.541269e+15   329.974814\n",
       "9         mnth_6  3.541269e+15   358.441707\n",
       "11        mnth_8  3.541269e+15   260.186161\n",
       "6         mnth_3  3.541269e+15   158.256991\n",
       "13       mnth_10  3.541269e+15   228.422168\n",
       "7         mnth_4  3.541269e+15    -0.717209\n",
       "5         mnth_2  3.541269e+15  -321.473059\n",
       "10        mnth_7  3.541269e+15  -219.802644\n",
       "4         mnth_1  3.541269e+15  -419.602984\n",
       "14       mnth_11  3.541269e+15  -539.626153\n",
       "15       mnth_12  3.541269e+15  -596.301365\n",
       "16  weathersit_1  2.457579e+15   917.687059\n",
       "17  weathersit_2  2.457579e+15   376.129076\n",
       "18  weathersit_3  2.457579e+15 -1293.816135\n",
       "25     weekday_6  8.319782e+14   222.343389\n",
       "19     weekday_0  8.319782e+14  -191.110834\n",
       "31    workingday  4.314588e+14   210.970710\n",
       "30       holiday  4.314588e+14  -242.203266\n",
       "24     weekday_5  4.005193e+14    84.232912\n",
       "22     weekday_3  4.005193e+14    59.300844\n",
       "23     weekday_4  4.005193e+14    62.856322\n",
       "21     weekday_2  4.005193e+14   -32.498610\n",
       "20     weekday_1  4.005193e+14  -205.124023\n",
       "26          temp  2.538546e+03  1708.293961\n",
       "32            yr  2.018267e+03  2018.537056\n",
       "27         atemp  1.084552e+03  1531.373934\n",
       "3       season_4  8.371710e+02   764.731124\n",
       "1       season_2  8.215287e+01   109.430160\n",
       "2       season_3 -1.453643e+02   -61.424018\n",
       "0       season_1 -7.788613e+02  -812.737266\n",
       "29     windspeed -1.199925e+03 -1047.237700\n",
       "28           hum -1.351891e+03 -1054.590078"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fs = pd.DataFrame({\"columns\":list(feat_names), \"coef_lr\":list((lr.coef_.T)), \"coef_ridge\":list((ridge.coef_.T))})\n",
    "fs.sort_values(by=['coef_lr'],ascending=False)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "可以看出，lr的系数都非常大，而ridge系数要好很多最后试试lasso"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The rmse of LassoRegression on train is  750.3338809143282\n",
      "The rmse of LassoRegression on test is  801.7720093590274\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\sika\\AppData\\Local\\Continuum\\anaconda3\\lib\\site-packages\\sklearn\\model_selection\\_split.py:2053: FutureWarning: You should specify a value for 'cv' instead of relying on the default value. The default value will change from 3 to 5 in version 0.22.\n",
      "  warnings.warn(CV_WARNING, FutureWarning)\n"
     ]
    }
   ],
   "source": [
    "lasso = LassoCV()  \n",
    "\n",
    "#3. 训练（内含CV）\n",
    "lasso.fit(X_train, y_train)  \n",
    "\n",
    "#4. 测试\n",
    "y_test_pred_lasso = lasso.predict(X_test)\n",
    "y_train_pred_lasso = lasso.predict(X_train)\n",
    "\n",
    "\n",
    "rmse_train=math.sqrt(mean_squared_error(y_train,y_train_pred_lasso))\n",
    "rmse_test=math.sqrt(mean_squared_error(y_test,y_test_pred_lasso))\n",
    "print(\"The rmse of LassoRegression on train is \",rmse_train)\n",
    "print(\"The rmse of LassoRegression on test is \",rmse_test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The r2_score of LassoRegression on train is  0.8486048634429293\n",
      "The r2_score of LassoRegression on test is  0.8332723819405335\n"
     ]
    }
   ],
   "source": [
    "print(\"The r2_score of LassoRegression on train is \",r2_score(y_train,y_train_pred_lasso))\n",
    "print(\"The r2_score of LassoRegression on test is \",r2_score(y_test,y_test_pred_lasso))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAaEAAAEKCAYAAAC7c+rvAAAAB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LgO+GWxJ9EJgMnAV8Ajg3bpnzgBVxn//Z3cuAmcB7zGxmF+MMASrcfQ7wHLG7HXTIcfd5wOfi2vcA7w39PwLcEde/HHjXiW+qyInRbXtEku984Jfu3gbsNrPngHNC+6/cvR3YZWbPxC0zDqiJ+/xhM7uJ2P9nxwHTgdWdxmkHHgjTPwfib3TZMb2CWPBB7HY8/2Fms4E24LS4/nuA8Se4nSInTCEkknzdPVTweA8bPAIUAJjZFOALwDnuXmtm93bM60H8Pbk67kbexpv/v/88sJvYHloW0BjXvyDUIJJUOhwnknzPAx8xs+xwnubdwEvAH4k9tC/LzMYQu3Foh3XAqWF6OHAIqA/9LutmnCxiN7IE+D9h/cdTCOwMe2IfJXYj2Q6nAWsT2DaRt0V7QiLJ92ti53teIbZ38kV332VmDwMXE/tjv4HY0zLrwzK/JxZKT7r7K2a2EqgENgF/6macQ8AMM1sR1vORHur6CfCwmV0DPBOW73BhqEEkqXQXbZEImdlQdz9oZqOI7R2dFwJqELFgOC+cS0pkXQfdfWgf1fU8sNDda/tifSLd0Z6QSLR+Z2ZFQB7wDXffBeDuR8zsdqAE2NqfBYVDhj9QAEl/0J6QiIhERhcmiIhIZBRCIiISGYWQiIhERiEkIiKRUQiJiEhkFEIiIhKZ/w/iSZ3KCXwZjgAAAABJRU5ErkJggg==\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "alpha is: 3.82310738833874\n"
     ]
    }
   ],
   "source": [
    "mses = np.mean(lasso.mse_path_, axis = 1)\n",
    "plt.plot(np.log10(lasso.alphas_), mses) \n",
    "#plt.plot(np.log10(lasso.alphas_)*np.ones(3), [0.3, 0.4, 1.0])\n",
    "plt.xlabel('log(alpha)')\n",
    "plt.ylabel('mse')\n",
    "plt.show()    \n",
    "            \n",
    "print ('alpha is:', lasso.alpha_)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>columns</th>\n",
       "      <th>coef_lr</th>\n",
       "      <th>coef_ridge</th>\n",
       "      <th>coef_lasso</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>mnth_9</td>\n",
       "      <td>3.541269e+15</td>\n",
       "      <td>762.241574</td>\n",
       "      <td>578.493208</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>mnth_5</td>\n",
       "      <td>3.541269e+15</td>\n",
       "      <td>329.974814</td>\n",
       "      <td>207.991241</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>mnth_6</td>\n",
       "      <td>3.541269e+15</td>\n",
       "      <td>358.441707</td>\n",
       "      <td>163.689798</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>mnth_8</td>\n",
       "      <td>3.541269e+15</td>\n",
       "      <td>260.186161</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>mnth_3</td>\n",
       "      <td>3.541269e+15</td>\n",
       "      <td>158.256991</td>\n",
       "      <td>191.328356</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>mnth_10</td>\n",
       "      <td>3.541269e+15</td>\n",
       "      <td>228.422168</td>\n",
       "      <td>222.295825</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>mnth_4</td>\n",
       "      <td>3.541269e+15</td>\n",
       "      <td>-0.717209</td>\n",
       "      <td>-0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>mnth_2</td>\n",
       "      <td>3.541269e+15</td>\n",
       "      <td>-321.473059</td>\n",
       "      <td>-134.707884</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>mnth_7</td>\n",
       "      <td>3.541269e+15</td>\n",
       "      <td>-219.802644</td>\n",
       "      <td>-429.042641</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>mnth_1</td>\n",
       "      <td>3.541269e+15</td>\n",
       "      <td>-419.602984</td>\n",
       "      <td>-219.126671</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>mnth_11</td>\n",
       "      <td>3.541269e+15</td>\n",
       "      <td>-539.626153</td>\n",
       "      <td>-416.014413</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15</th>\n",
       "      <td>mnth_12</td>\n",
       "      <td>3.541269e+15</td>\n",
       "      <td>-596.301365</td>\n",
       "      <td>-446.566688</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>16</th>\n",
       "      <td>weathersit_1</td>\n",
       "      <td>2.457579e+15</td>\n",
       "      <td>917.687059</td>\n",
       "      <td>536.983755</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17</th>\n",
       "      <td>weathersit_2</td>\n",
       "      <td>2.457579e+15</td>\n",
       "      <td>376.129076</td>\n",
       "      <td>-0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>18</th>\n",
       "      <td>weathersit_3</td>\n",
       "      <td>2.457579e+15</td>\n",
       "      <td>-1293.816135</td>\n",
       "      <td>-1656.938823</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25</th>\n",
       "      <td>weekday_6</td>\n",
       "      <td>8.319782e+14</td>\n",
       "      <td>222.343389</td>\n",
       "      <td>2.953440</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>19</th>\n",
       "      <td>weekday_0</td>\n",
       "      <td>8.319782e+14</td>\n",
       "      <td>-191.110834</td>\n",
       "      <td>-355.511941</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>31</th>\n",
       "      <td>workingday</td>\n",
       "      <td>4.314588e+14</td>\n",
       "      <td>210.970710</td>\n",
       "      <td>52.602592</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>30</th>\n",
       "      <td>holiday</td>\n",
       "      <td>4.314588e+14</td>\n",
       "      <td>-242.203266</td>\n",
       "      <td>-301.730848</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>24</th>\n",
       "      <td>weekday_5</td>\n",
       "      <td>4.005193e+14</td>\n",
       "      <td>84.232912</td>\n",
       "      <td>15.695535</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>22</th>\n",
       "      <td>weekday_3</td>\n",
       "      <td>4.005193e+14</td>\n",
       "      <td>59.300844</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23</th>\n",
       "      <td>weekday_4</td>\n",
       "      <td>4.005193e+14</td>\n",
       "      <td>62.856322</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>21</th>\n",
       "      <td>weekday_2</td>\n",
       "      <td>4.005193e+14</td>\n",
       "      <td>-32.498610</td>\n",
       "      <td>-48.560524</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20</th>\n",
       "      <td>weekday_1</td>\n",
       "      <td>4.005193e+14</td>\n",
       "      <td>-205.124023</td>\n",
       "      <td>-226.821264</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>26</th>\n",
       "      <td>temp</td>\n",
       "      <td>2.538546e+03</td>\n",
       "      <td>1708.293961</td>\n",
       "      <td>2697.696415</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>32</th>\n",
       "      <td>yr</td>\n",
       "      <td>2.018267e+03</td>\n",
       "      <td>2018.537056</td>\n",
       "      <td>2018.064425</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>27</th>\n",
       "      <td>atemp</td>\n",
       "      <td>1.084552e+03</td>\n",
       "      <td>1531.373934</td>\n",
       "      <td>857.276640</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>season_4</td>\n",
       "      <td>8.371710e+02</td>\n",
       "      <td>764.731124</td>\n",
       "      <td>579.808352</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>season_2</td>\n",
       "      <td>8.215287e+01</td>\n",
       "      <td>109.430160</td>\n",
       "      <td>3.475120</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>season_3</td>\n",
       "      <td>-1.453643e+02</td>\n",
       "      <td>-61.424018</td>\n",
       "      <td>-75.481436</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>season_1</td>\n",
       "      <td>-7.788613e+02</td>\n",
       "      <td>-812.737266</td>\n",
       "      <td>-1023.160420</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>29</th>\n",
       "      <td>windspeed</td>\n",
       "      <td>-1.199925e+03</td>\n",
       "      <td>-1047.237700</td>\n",
       "      <td>-1012.098234</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>28</th>\n",
       "      <td>hum</td>\n",
       "      <td>-1.351891e+03</td>\n",
       "      <td>-1054.590078</td>\n",
       "      <td>-1004.243380</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "         columns       coef_lr   coef_ridge   coef_lasso\n",
       "12        mnth_9  3.541269e+15   762.241574   578.493208\n",
       "8         mnth_5  3.541269e+15   329.974814   207.991241\n",
       "9         mnth_6  3.541269e+15   358.441707   163.689798\n",
       "11        mnth_8  3.541269e+15   260.186161     0.000000\n",
       "6         mnth_3  3.541269e+15   158.256991   191.328356\n",
       "13       mnth_10  3.541269e+15   228.422168   222.295825\n",
       "7         mnth_4  3.541269e+15    -0.717209    -0.000000\n",
       "5         mnth_2  3.541269e+15  -321.473059  -134.707884\n",
       "10        mnth_7  3.541269e+15  -219.802644  -429.042641\n",
       "4         mnth_1  3.541269e+15  -419.602984  -219.126671\n",
       "14       mnth_11  3.541269e+15  -539.626153  -416.014413\n",
       "15       mnth_12  3.541269e+15  -596.301365  -446.566688\n",
       "16  weathersit_1  2.457579e+15   917.687059   536.983755\n",
       "17  weathersit_2  2.457579e+15   376.129076    -0.000000\n",
       "18  weathersit_3  2.457579e+15 -1293.816135 -1656.938823\n",
       "25     weekday_6  8.319782e+14   222.343389     2.953440\n",
       "19     weekday_0  8.319782e+14  -191.110834  -355.511941\n",
       "31    workingday  4.314588e+14   210.970710    52.602592\n",
       "30       holiday  4.314588e+14  -242.203266  -301.730848\n",
       "24     weekday_5  4.005193e+14    84.232912    15.695535\n",
       "22     weekday_3  4.005193e+14    59.300844     0.000000\n",
       "23     weekday_4  4.005193e+14    62.856322     0.000000\n",
       "21     weekday_2  4.005193e+14   -32.498610   -48.560524\n",
       "20     weekday_1  4.005193e+14  -205.124023  -226.821264\n",
       "26          temp  2.538546e+03  1708.293961  2697.696415\n",
       "32            yr  2.018267e+03  2018.537056  2018.064425\n",
       "27         atemp  1.084552e+03  1531.373934   857.276640\n",
       "3       season_4  8.371710e+02   764.731124   579.808352\n",
       "1       season_2  8.215287e+01   109.430160     3.475120\n",
       "2       season_3 -1.453643e+02   -61.424018   -75.481436\n",
       "0       season_1 -7.788613e+02  -812.737266 -1023.160420\n",
       "29     windspeed -1.199925e+03 -1047.237700 -1012.098234\n",
       "28           hum -1.351891e+03 -1054.590078 -1004.243380"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fs = pd.DataFrame({\"columns\":list(feat_names), \"coef_lr\":list((lr.coef_.T)), \"coef_ridge\":list((ridge.coef_.T)), \"coef_lasso\":list((lasso.coef_.T))})\n",
    "fs.sort_values(by=['coef_lr'],ascending=False)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "LassoCV在测试集上表现最好，而且测试集与训练集效果接近，过拟合情况较少（最小二乘线性回归就属于过拟合了，训练集表现最好，可是测试集效果降低很多，参数还被调的特别大）"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
